local BuffPanel = require "View/Expedition/SubPanel/ExpeditionBuffPanel"

---@class UIExpeditionCardPreview : UIBase
UIExpeditionCardPreview = UIBase.New(PanelResNames.UIExpeditionCardPreview)
local t = UIExpeditionCardPreview

function t.Init(type)
    t.type = type or 1
    UIBase.Init(t)
end

function t:OnCreate()
    self.buffTipText = self:GetChild("BuffTips")
    self.buffNameText = self:GetChild("BuffName")
    self.buffIcon = self:GetChild("Bufficon")
    self.BgClose = self:GetChild("BgClose")
    self.ctrlType = self:GetController("type")
    self.ctrlNothing = self:GetController("nothing")
    self.cardList = self:GetChild("List_Card")
    self:SetDynamicList(self.cardList,self.ItemRender,nil,true)
    self:RegisterOnClick(self.BgClose,self.ClickClose)
end

function t:OnEnable()
    self:Refresh()
end

function t:ItemRender(idx,obj)
    local panel = self:CreateComponent(BuffPanel,obj)
    local id = self.buffList[idx]
    panel:RefreshBuff(id)
end
function t:OnDestroy()
end
function t:Refresh()
    self.buffList = {};
    if self.type == 1 then
        if ExpeditionMgr.mapBuffId == 0 then
            self.buffNameText.text = ""
            self.buffTipText.text = ""
            self.buffIcon.url = ""
        else
            ObjUtil.SetBuffIcon(ExpeditionMgr.mapBuffId, self.buffIcon, self.buffNameText, self.buffTipText)
        end
        self.buffList = ExpeditionMgr.stageBuffList;
        if ExpeditionMgr.mapBuffId == 0 and #ExpeditionMgr.stageBuffList == 0 then
            self.ctrlNothing.selectedIndex = 1
        else
            self.ctrlNothing.selectedIndex = 0
        end
    elseif self.type == 2 then
        self.buffNameText.text = "";
        self.buffTipText.text = "";
        self.buffIcon.url = "";
        self.buffList = logicMgr.LongJourneyHellManager.getBuffList();
        self.ctrlNothing.selectedIndex = 1;
    end
    local count = #self.buffList;
    self.cardList.numItems = count;
    if count > 0 then
        self.ctrlType.selectedIndex = 1;
    else
        self.ctrlType.selectedIndex = 0;
    end
end
function t:ClickClose()
    closeUI(PanelResNames.UIExpeditionCardPreview)
end

return t